<?php
//活动表

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;

class DealModel extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'deal';

    const DEAL_BUY = 1;
    const DEAL_SALE = 2;

    const DEAL_TYPES = [
        self::DEAL_BUY => '买入',
        self::DEAL_SALE => '卖出',
    ];

    const MARKET_INDEX_UP = 10;   //10为上涨中，20为上涨回调，30为横盘上涨，40为横盘中，50为横盘下跌，60为下跌中，70为下跌中回调 // 1为上涨，2为横盘，3为下跌
    const MAEKRT_INDEX_UP_CALLBACK = 20;
    const MAEKRT_INDEX_HORIZONTAL_UP = 30;
    const MAEKRT_INDEX_HORIZONTAL = 40;
    const MAEKRT_INDEX_HORIZONTAL_DOWN = 50;
    const MARKET_INDEX_DOWN = 60;
    const MARKET_INDEX_DOWN_UP = 70;

    const MARKET_INDEX_STATUSES = [
      self::MARKET_INDEX_UP =>  '上涨中',
      self::MAEKRT_INDEX_UP_CALLBACK =>  '上涨回调',
      self::MAEKRT_INDEX_HORIZONTAL_UP =>  '横盘上涨',
      self::MAEKRT_INDEX_HORIZONTAL =>  '横盘',
      self::MAEKRT_INDEX_HORIZONTAL_DOWN =>  '横盘下跌',
      self::MARKET_INDEX_DOWN =>  '下跌',
      self::MARKET_INDEX_DOWN_UP =>  '下跌中回调',
    ];

    const IS_SHORT_COVERING = 1;        // 1为是，2为否
    const IS_NOT_SHORT_COVERING = 2;

    const COVERIN = [
        self::IS_SHORT_COVERING => '是',
        self::IS_NOT_SHORT_COVERING => '否',
    ];

    const STOCK_TYPE_SH = 1;    // 1为上海股票，2为深圳股票，3为科创板，4为创业板，5为北交所
    const STOCK_TYPE_SZ = 2;
    const STOCK_TYPE_KCB = 3;
    const STOCK_TYPE_CYB = 4;
    const STOCK_TYPE_BJS = 5;

    const STOCK_TYPE_KEYS = [
        'sh' => self::STOCK_TYPE_SH,
        'sz' => self::STOCK_TYPE_SZ,
    ];

    const STOCK_TYPES = [
        self::STOCK_TYPE_SH => '上海股票',
        self::STOCK_TYPE_SZ => '深圳股票',
        self::STOCK_TYPE_KCB => '科创板',
        self::STOCK_TYPE_CYB => '创业板',
        self::STOCK_TYPE_BJS => '北交所'
    ];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    // 访问器
    protected function price(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    /*protected function salePrice(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }*/

    public function operate ()
    {
        return $this->belongsTo(OperateModel::class, 'operate_id', 'id');
    }
}
